package DataStructures;

//3.11

public class SingleLinkedList<T> {
	private int theSize;
	private Node<T> header;
	
	private static class Node<T> {
		public Node(T d, Node<T> n) {
			data = d;
			next = n;
		}
		
		public T data;
		public Node<T> next;
	}
	
	public SingleLinkedList() {
		theSize = 0;
		header = new Node<T>(null, null);
	}
	
	public int size() {
		return theSize;
	}
	
	public void printList() {
		Node<T> tmpNode = header.next;
		
		while(tmpNode != null) {
			System.out.println(tmpNode.data);
			tmpNode = tmpNode.next;
		}
	}
	
	public boolean contains(T x) {
		Node<T> tmpNode = header.next;
		
		while(tmpNode != null && !tmpNode.data.equals(x))
			tmpNode = tmpNode.next;
		if(tmpNode != null)
			return true;
		else
			return false;
	}
	
	public void addIfNot(T x) {
		Node<T> tmpNode = header;
		
		while(tmpNode.next != null && !tmpNode.next.data.equals(x))
			tmpNode = tmpNode.next;
		if(tmpNode.next == null) {
			Node<T> newNode = new Node<T>(x, null);
			tmpNode.next = newNode;
		}
	}
	
	public void deleteIfHas(T x) {
		Node<T> tmpNode = header;
		
		while(tmpNode.next != null && !tmpNode.next.data.equals(x))
			tmpNode = tmpNode.next;
		if(tmpNode.next != null)
			tmpNode.next = tmpNode.next.next;
	}
}
